package com.magiccompass.alipay.barcode.service;

import java.util.List;

import javax.annotation.Resource;

import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.magiccompass.alipay.barcode.entity.UserInfo;

@Service("userService")
@Transactional
public class UserServiceImpl implements UserService {
	@Resource(name="sessionFactory")
	private SessionFactory sessionFactory;
	
	public UserServiceImpl(){}
	@Override
	public UserInfo findUserInfoByUserName(String userName){
		Session session = sessionFactory.getCurrentSession();
		String hql = "FROM UserInfo ui WHERE ui.userName = :userName order by id desc";
		Query query = session.createQuery(hql);
		query.setParameter("userName",userName);
		List<UserInfo> results = query.list();
		if(results != null && results.size()>0){
			return results.get(0);
		}else{
			return null;
		}
	}
	@Override
	public UserInfo findUserInfoByUserId(Integer id){
		Session session = sessionFactory.getCurrentSession();
		String hql = "FROM UserInfo ui WHERE ui.id = :id order by id desc";
		Query query = session.createQuery(hql);
		query.setParameter("id",id);
		List<UserInfo> results = query.list();
		if(results != null && results.size()>0){
			return results.get(0);
		}else{
			return null;
		}
	}
}